package sorted;

public class DeadLock {
    private static Object object1 = new Object();
    private static Object object2 = new Object();

    public static void main(String[] args) {
        new Thread(() -> {
            synchronized (object1) {
                System.out.println(Thread.currentThread() + " get object1");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread() + " waiting get object12");
                synchronized (object2) {
                    System.out.println(Thread.currentThread() + " get object2");
                }
            }
        }, "线程 1").start();
        new Thread(()->{
            synchronized (object1){
                System.out.println(Thread.currentThread().getName() + " get Object2");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+" ready get Object1");
                synchronized (object2){
                    System.out.println(Thread.currentThread().getName()+" get object1");
                }
            }
        },"线程 2").start();
    }
}
